<?php
/**
 * Check authority
 *
 * @author     Mario Schillermann <info@rockit-flash.com>
 * @encoding   UTF-8 äöüßÖÄÜ
 * @copyright  © 2011 Mario Schillermann (info@rockit-flash.com)
 * @license    GNU GPL v2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
 * @uses
 * @category   Library
 * @package    App
 * @subpackage Security
 * @todo
 */

class App_Security_Auth extends App_Session_Abstract
{
    /**
     * Session namespace
     */
    protected $_namespace = "App_Security_Auth";
 
    /**
     * User id
     * 
     * @var integer 
     */
    protected $_userId = null;
    
    /**
     * Username
     * 
     * @var string 
     */
    protected $_username = null;
    
    /**
     * Password
     * 
     * @var string 
     */
    protected $_password = null;
    
    /**
     * Role id
     * 
     * @var integer 
     */
    protected $_roleId = null;
    
    /**
     * Rolename
     * 
     * @var string 
     */
    protected $_rolename = null;

    /**
     * Persistent storage handler
     *
     * @var Zend_Auth_Storage_Interface
     */
    protected $_storage = null;
    
    private static $instance = null;
    
    private function __clone() {}
    
    /**
     * Get last identity of the session
     * 
     */
    protected function __construct() {
        $this->_getIdentity();
    }

    
    public static function getInstance() {
        if (self::$instance == null) {
            self::$instance = new App_Security_Auth();
        }
        return self::$instance;
    }

    
    /**
     * Set username
     * 
     * @param string $username
     * 
     * @return App_Security_Auth|boolean
     */
    public function setUsername($username)
    {
        $this->_username = $username;
        if (empty($this->_password)) {
            return $this; 
        } else {
            return $this->isVaild();
        }
    }
    
    /**
     * Set Password
     * 
     * @param string $password
     * @return App_Security_Auth|boolean
     */
    public function setPassword($password)
    {
        $this->_password = md5($password);
        if (empty($this->_username)) {
            return $this; 
        } else {
            return $this->isVaild();
        }
    }
    
    /**
     * Set role id
     * 
     * @param integer $roleId 
     * 
     * @return void
     */
    protected function _setRoleId($roleId)
    {
        $this->_roleId = (integer) $roleId;
    }
    
    /**
     * Set user id
     * 
     * @param integer $userId
     * 
     * @return void
     */
    protected function _setUserId($userId)
    {
        $this->_userId = (integer) $userId;
    }
    
    /**
     * Set rolename
     * 
     * @param string $rolename
     * 
     * @return void
     */
    protected function _setRolename($rolename)
    {
        $this->_rolename = $rolename;
    }
    
    /**
     * Get rolename
     * 
     * @return string 
     */
    public function getRolename()
    {
        return $this->_rolename;
    }
    
    /**
     * Get user id
     * 
     * @return integer 
     */
    public function getUserId()
    {
        return $this->_userId;
    }
    
    /**
     * Get role id
     *
     * @return integer 
     */
    public function getRoleId()
    {
        return $this->_roleId;
    }
    
    /**
     * Get username
     * 
     * @return string 
     */
    public function getUsername()
    {
        return $this->_username;
    }
    
    /**
     * Get Password
     * 
     * @return string 
     */
    public function getPassword()
    {
        return $this->_password;
    }
    
    /**
     * Check authentication
     * 
     * @return boolean 
     */
    public function isVaild()
    {
        $users = new App_Model_Database_Users();
        $userIdentity = $users->getIdentity(
            $this->getUsername(),
            $this->getPassword()
        );

        if ($userIdentity === false) {
            $this->clearIdentity();
            return false;
        }
 
        $this->_setUserId($userIdentity["userId"]);
        $this->_setRoleId($userIdentity["roleId"]);
        $this->_setRolename($userIdentity["rolename"]);
        
        $this->_setIdentity();

        return true;
    }

    /**
     * Clear identity
     * 
     * @return void
     */
    public function destroyIdentity()
    {
        $this->_username = null;
        $this->setPassword(null);
        
        session_destroy();
    }
    
    /**
     * Clear identity
     * 
     * @return void
     */
    public function clearIdentity() {
        
        parent::clear();
    }

        /**
     * Get identity of the session
     * 
     * @return boolean 
     */
    protected function _getIdentity()
    {
        $storage = $this->_getStorage();

        if (!empty($storage->username)) { error_log("---full----");
            $this->setUsername($storage->username);
            $this->setPassword($storage->password);
            $this->_setUserId($storage->userId);
            $this->_setRoleId($storage->roleId);
            $this->_setRolename($storage->rolename);
        } error_log("---empty----");
    }
    
    /**
     * Stores identity in the session
     * 
     * @return void
     */
    protected function _setIdentity()
    {
        $storage = $this->_getStorage();

        $storage->username = $this->getUsername();
        $storage->password = $this->getPassword();
        $storage->userId = $this->getUserId();
        $storage->roleId = $this->getRoleId();
        $storage->rolename = $this->getRolename();
    }

}
